// @ts-nocheck
import React, { FC, useState, useEffect, useMemo } from "react";
import { useNavigate } from "react-router-dom";
// service
import {
  NavBar,
  Dialog,
  Form,
  Input,
  Cascader,
  TextArea,
  ImageUploader,
  Toast,
} from "antd-mobile";
import { getCurrentUser } from "@/utils/authority";
import { opinionPublicity, addPointViewVote } from "../service";

import "./index.less";



function ViewDetails() {
  const userId = getCurrentUser()?.data?.user_id;
  const navigate = useNavigate();
  const [residentDeliberationId, setResidentDeliberationId] = useState<string>("")
  const [pointViewId, setPointViewId] = useState<string>("")
  const [dataContent, setDataContent] = useState({})
  const [user, setUser] = useState<any>({})

  useEffect(() => {
    const [residentDeliberationId, pointViewId] = location?.search?.substring(1).split("&&")
    setResidentDeliberationId(residentDeliberationId.split("=")?.[1]);
    setPointViewId(pointViewId.split("=")?.[1])
  }, [location])

  useEffect(() => {
    if (residentDeliberationId && pointViewId) {
      opinionPublicity({ villageCode: "511602006004", residentDeliberationId: residentDeliberationId/* classType:selectType  */ })
        .then((res) => {
          if (res.code == 200) {
            const filterData = res.data?.filter((item: any) => item?.id == pointViewId)
            const data = filterData?.[0]
            setDataContent(data);

            // setDataList(res?.data);
          }
        })
        .catch((error) => {
          console.log(error);
        });
    }
  }, [residentDeliberationId, pointViewId])
  useEffect(() => {
    const users = JSON.parse(
      localStorage.getItem("users") || JSON.stringify({})
    );
    setUser(users)
  }, [])
  return (
    <div className="ViewDetails-container">
      <div className="header-bg">
        <NavBar
          onBack={() => navigate(`/intelligentGovernance/theTownHall/publicityOfIdeas/?id=${residentDeliberationId}`)}>
          <div className="commontitle">观点详情</div>
        </NavBar>
      </div>
      <div className="ViewDetails-content">
        <div className="viewpointInformation">
          <div className="viewpointInformation-photo">
            <img src={dataContent.img || `/images/userPhoto/photo1.png`} alt="" />
          </div>
          <div className="viewpointInformation-name">{dataContent.pointViewPeopleName}</div>
          <div className="viewpointInformation-time">{dataContent.createTime}</div>
        </div>
        <div className="viewpointContent">{dataContent?.pointViewContent}</div>
        <div className="voteBox"
          onClick={(e) => {
            Dialog.confirm({
              content: '是否投票该观点',
              onConfirm: async () => {
                const params = {
                  residentDeliberationId: residentDeliberationId,
                  pointViewId: pointViewId,
                  votePeopleId: user?.user_id || "",
                  votePeopleIdCard: user?.IdCard || "",
                  votePeoplePhone: user?.account || "",
                  votePeopleName: user?.real_name || "",
                }
                let res = await addPointViewVote(params)
                if (res?.code == 200) {
                  Toast.show({
                    icon: 'success',
                    content: '投票成功',
                    position: 'bottom',
                  })
                  navigate(`/intelligentGovernance/theTownHall/publicityOfIdeas/?id=${residentDeliberationId}`)
                }else{
                  Toast.show({
                    icon: 'success',
                    content: '投票失败',
                    position: 'bottom',
                  })
                }
              },
            })
            e.stopPropagation();
          }
          }
        >
          投票
        </div>
      </div>

    </div>
  );
}

export default ViewDetails;
